﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using permit.App_Code.Entity;
using permit.App_Code.DataAccess;
using System.Data;

namespace permit.opPermit
{
    public partial class queryPermit : System.Web.UI.Page
    {
        DepartmentAccess depAcc = new DepartmentAccess();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                 UserTable user = (UserTable)Session["UserTable"];
                if (user == null)
                {
                    Response.Write("<script>alert('请先登录！')</script>");
                    Response.Redirect("~/default.aspx");
                }

               
                if (Request.QueryString["Sturt"] == "no")
                {
                    lblInfo.Text = "没有该条记录！";
                }
                DataSet ds = depAcc.GetDepartments();

                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    if (dr["dep_AreaCode"].ToString() == user.DEP_AREACODE && user.DEP_AREACODE != "620100")
                    {
                        ddlDepArea.Items.Add(new ListItem(dr["dep_Name"].ToString(), dr["dep_AreaCode"].ToString()));
                        break;
                    }
                    if (user.DEP_AREACODE == "620100")
                    {
                        if (ddlDepArea.Items.Count == 0)
                        {
                            ddlDepArea.Items.Add(new ListItem("请选择", "0"));
                        }

                        ddlDepArea.Items.Add(new ListItem(dr["dep_Name"].ToString(), dr["dep_AreaCode"].ToString()));
                    }
                    
                }
            }
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            
            PermitTable permit = new PermitTable();

            permit.Per_Department = txtDepartment.Text; //单位名称
            permit.Per_Address = txtAddress.Text;  //地址
            permit.Per_PermitCode = txtPerCode.Text; //甘餐证字
            permit.Per_Type = txtPerType.Text;  //类别
            if (txtRep.Text != "")
            {
                permit.Per_Representative = txtRep.Text + '（' + rblRep.SelectedValue + '）';
            }
            else
            {
                permit.Per_Representative = "";
            }
            permit.Per_Organ = txtOrgan.Text;        //发证机关
            

            if (txtPerDate.Text != "")
            {
                permit.PermitDate = Convert.ToDateTime(txtPerDate.Text);  //发证日期
            }
            if (txtEndDate.Text != "")
            {
                permit.Per_EndDate = Convert.ToDateTime(txtEndDate.Text);  //有效开始日期
            }
            if (txtStarDate.Text != "")
            {
                permit.Per_StarDate = Convert.ToDateTime(txtStarDate.Text); //有效截止日期
            }

            permit.Per_Count = rblPrint.SelectedIndex; //是否打印  0 是， 1 否
            
            if (!MeetingManagerLib.DataTypeCheck.IsDigit(txtNumber.Text))
            {
                Page.RegisterStartupScript("this5", "<script>alert('从业人数必须是数字！')</script>");
                return;
            }
            if (txtNumber.Text != "")
            {
                permit.Per_Number = Convert.ToInt32(txtNumber.Text);
            }

            if (!MeetingManagerLib.DataTypeCheck.IsDigit(txtArea.Text))
            {
                Page.RegisterStartupScript("this5", "<script>alert('经营面积必须是数字！')</script>");
                return;
            }
            if (txtArea.Text != "")
            {
                permit.Per_Area = double.Parse(txtArea.Text);
            }

            if (ddlDepArea.SelectedIndex >= 0 && ddlDepArea.SelectedValue != "0")
            {
                permit.Per_AreaCode = ddlDepArea.SelectedValue;
            }
            else
            {
                permit.Per_AreaCode = "";
            }
            
            if (Session["Permit"] != null)
            {
                Session["Permit"] = null;
            }
            Session["Permit"] = permit;

            Response.Redirect("DisplayPermit.aspx?print="+ rblPrint.SelectedIndex.ToString());

          


        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            txtDepartment.Text = "";
            txtAddress.Text = "";
            txtPerCode.Text = "";
            txtPerType.Text = "";
            txtRep.Text = "";
            txtOrgan.Text = "";
            txtPerDate.Text = "";       
            txtEndDate.Text=  "";         
            txtStarDate.Text= "";
            txtNumber.Text = "";
            txtArea.Text = "";
            rblPrint.SelectedIndex = -1;
        }

    }
}
